<?php
/**
 * Created by PhpStorm.
 * User: Liulibao
 * Date: 2019/11/9
 * Time: 17:25
 */

namespace app\admin\controller;


use app\common\model\Brand;
use app\common\model\Goods;
use app\common\model\Goodsclass;
use app\common\model\InquiryHistoryModel;
use app\common\model\InquiryInfoModel;
use app\common\model\InquiryModel;
use app\common\model\InquiryStationModel;
use app\common\model\Storeclass;
use think\Lang;
use think\Request;

class Goodinquiry extends AdminControl
{
    public function _initialize() {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/good_inquiry.lang.php');
    }

    ############################ 询价列表 #################################################
    public function inquiry_info()
    {
        $inquiry_list = (new InquiryModel())->getInquiryList();
        $station = (new InquiryStationModel())->getStationByInquiryId(input('param.inquiry_id', 0));
        $station_list = (new InquiryStationModel())->getStationByInquiryList();

        $config_list = InquiryInfoModel::$goodConfig;
        $inquiryInfoModel = new InquiryInfoModel();
        $condition = array();
        if(!empty(input('param.goods_name'))) {
            $condition['goodscommon.goods_name'] = array('like', "%".input('param.goods_name')."%");
        }
        if(!empty(input('param.store_name'))) {
            $condition['goodscommon.store_name'] = array('like', "%".input('param.store_name')."%");
        }
        if(!empty(input('param.inquiry_id'))) {
            $condition['info.inquiry_id'] = input('param.inquiry_id');
        }
        if(!empty(input('param.station_id'))) {
            $condition['info.station_id'] = input('param.station_id');
        }
        if(!empty(input('param.config_id'))) {
            $condition['info.good_config'] = array('like', "%".input('param.config_id')."%");
        }
        $info = $inquiryInfoModel->getInquiryList($condition, 10);

        $this->assign('info_list', $info);
        $this->assign('inquiry_list', $inquiry_list);
        $this->assign('config_list', $config_list);
        $this->assign('station', $station);
        $this->assign('station_list', $station_list);
        $this->setAdminCurItem('inquiry_info');
        return $this->fetch();
    }

    public function inquiry_info_add()
    {
        $choose_gcid = ($t = intval(input('param.choose_gcid'))) > 0 ? $t : 0;
        $goodsClass = (new Goodsclass())->getGoodsclassCache($choose_gcid, 3);
        $this->assign('gc_json', json_encode($goodsClass['showclass']));
        $this->assign('gc_choose_json', json_encode($goodsClass['choose_gcid']));

        /**
         * 查询条件
         */
        $where = array();
        $search_goods_name = trim(input('param.search_goods_name'));
        if ($search_goods_name != '') {
            $where['goods_name'] = array('like', '%' . $search_goods_name . '%');
        }

        if ($choose_gcid > 0) {
            $where['gc_id_' . ($goodsClass['showclass'][$choose_gcid]['depth'])] = $choose_gcid;
        }

        // $where['is_temlate'] = 0;  #是否是模板
        $where['goods_lock'] = 0; #商品没有被删除
        $search_commonid = intval(input('param.search_commonid'));
        if ($search_commonid > 0) {
            $where['goods_commonid'] = $search_commonid;
        }
        $where['goods_status'] = 1;
        $goods_model = new Goods();
        $goods_list = $goods_model->getGoodsCommonList($where, '*', 10, 'mall_goods_commend desc,mall_goods_sort asc');
//        var_dump($goods_list);die;
        $this->assign('goods_list', $goods_list);
        $this->assign('show_page', $goods_model->page_info->render());

        // 品牌
        $brand_list = (new Brand())->getBrandPassedList();

        $this->assign('search', $where);
        $this->assign('brand_list', $brand_list);

        // 询价商店分类
        $inquiry_list =  (new InquiryModel())->getInquiryList();
        $this->assign('inquiry_list', $inquiry_list);

        // 配置信息
        $config_list = InquiryInfoModel::$goodConfig;
        $this->assign('config_list', $config_list);

        $this->setAdminCurItem('inquiry_list');
        return $this->fetch();
    }

    /**
     * @description: 展示工位店铺信息  @MrLiu
     */
    public function show_param()
    {
        $goods_id = intval(input('goods_id'));
        $config_list = InquiryInfoModel::$goodConfig;
        $inquiry_list =  (new InquiryModel())->getInquiryList();

        $this->assign('inquiry_list', $inquiry_list);
        $this->assign('config_list', $config_list);
        $this->assign('goods_id', $goods_id);

        $this->setAdminCurItem('show_param');
        return $this->fetch();
    }

    public function import_inquiry()
    {
        $data = array(
            'inquiry_id' => input('param.inquiry_id', ''),
            'station_id' => input('param.station_id', ''),
            'goods_id' => input('param.goods_id', ''),
            'good_config' => input('param.good_config', ''),
            'num' => input('param.num', ''),
        );

        if (empty($data['inquiry_id'])) {
            $this->error('店铺类型不可为空！');
        }
        if (empty($data['station_id'])) {
            $this->error('工位不可为空！');
        }
        if (empty($data['good_config'])) {
            $this->error('配置不可为空！');
        }
        if (empty($data['num']) &&!($data['num']>0)){
            $this->error('请输入正确的数量！');
        }
        

        $inquiryInfoModel = new InquiryInfoModel();
        $cont = $inquiryInfoModel->getOne($data);
        if (!empty($cont)) {
            $this->error('同配置的商品已经存在，请不要再次添加');
        }
        $status = $inquiryInfoModel->add($data);
        if ($status>=0) {
            $this->success('导入成功');
        } else {
            $this->error(lang('edit_fail'));
        }
    }

    ############################ 店铺分类 #################################################

    public function index(InquiryModel $inquiryModel)
    {
        $where = array();
      if (!empty(input('param.inquiry_name'))) {
            $where['inquiry_name'] = input('param.inquiry_name');
        }
        $inquiry_list = $inquiryModel->getInquiryList($where, 10);
        $this->assign('inquiry_list', $inquiry_list);
        $this->setAdminCurItem('index');
        return $this->fetch();
    }


    public function inquiry_add(InquiryModel $inquiryModel)
    {
        $inquiry= $inquiryModel->getOneById(input('param.id'));
        $this->assign('inquiry', $inquiry);
        $this->setAdminCurItem('inquiry_add');
        return $this->fetch('inquiry_add');
    }


    public function store(InquiryModel $inquiryModel)
    {
        $data = array(
            'inquiry_name' => input('param.inquiry_name', '')
        );

        if(empty(input('param.id'))) {// 编辑
            $status = $inquiryModel->add($data);
        } else {
            $status = $inquiryModel->edit($data, input('param.id'));
        }

        if ($status>=0) {
            $this->log(lang('ds_edit') . lang('inquiry_station_add') . '[' . $data['inquiry_name'] . ']', 1);
            dsLayerOpenSuccess('保存成功');
        } else {
            $this->error(lang('edit_fail'));
        }
    }

    public function inquiry_del()
    {
        $id = intval(input('param.id'));
        $status = (new InquiryInfoModel())->del($id);
        if ($status === false) {
            ds_json_encode(10001, lang('del_fail'));
        }
        $this->log(lang('ds_add') . lang('inquiry_station_list') . '[ID:' . $id . ']', 1);
        ds_json_encode(10000, lang('del_success'));
    }


//    ############################### 工位设置  ####################################################
    public function station_list(InquiryStationModel $inquiryStationModel)
    {
        $condition = array('inq.is_del' => 0);
        if(!empty(input('param.search_station_name'))) {
            $condition['inq.station_name'] = array('like', "%". input('param.search_station_name') ."%");
        }
        $station = $inquiryStationModel->getStationList($condition);
        $this->assign('station_list', $station);
        $this->setAdminCurItem('inquiry_station_list');
        return $this->fetch('inquiry_station_list');
    }

    public function inquiry_station_add(
        Request $request,
        InquiryStationModel $inquiryStationModel,
        InquiryModel $inquiryModel)
    {
        if ($request->isPost()) {
            $data = array(
                'station_name' => input('param.station_name', ''),
                'status' => input('param.status', 1),
                'inquiry_id' => input('param.inquiry_id', 0),
            );
            if (empty(input('param.station_id'))) {
                $status = $inquiryStationModel->add($data);
            } else {
                $status = $inquiryStationModel->edit($data, input('param.station_id'));
            }

            if ($status>=0) {
                $this->log(lang('ds_edit') . lang('inquiry_station_add') . '[' . $data['station_name'] . ']', 1);
                dsLayerOpenSuccess(lang('edit_success'));
            } else {
                $this->error(lang('edit_fail'));
            }
        } else {
            $inquiry_id = input('param.inquiry_id', 0);
            $inquiry = $inquiryModel->getInquiryList();
            $stations = [];
            if (!empty(input('param.station_id'))) {
                $stations = $inquiryStationModel->getOneById(input('param.station_id'));
            }

            $this->assign('stations', $stations);
            $this->assign('inquiry', $inquiry);
            $this->assign('inquiry_id', $inquiry_id);
            $this->setAdminCurItem('inquiry_station');
            return $this->fetch('inquiry_station_add');
        }
    }

    public function station_del(InquiryStationModel $inquiryStationModel)
    {
        $station_id = intval(input('param.station_id'));
        $status = $inquiryStationModel->del($station_id);
        if ($status === false) {
            ds_json_encode(10001, lang('del_fail'));
        }
        $this->log(lang('ds_add') . lang('inquiry_station_list') . '[ID:' . $station_id . ']', 1);
        ds_json_encode(10000, lang('del_success'));
    }

    ############################# 获取询价历史记录 ################################

    public function inquiry_history(InquiryHistoryModel $inquiryHistoryModel)
    {
        $condition = array();
        if(!empty(input('param.user_type'))) {
            $condition['history.type'] = input('param.user_type');
        }

        if(!empty(input('param.goods_name'))) {
            $condition['goodscommon.goods_name'] = input('param.goods_name');
        }
        $config_list = InquiryInfoModel::$goodConfig;
        $info_list = $inquiryHistoryModel->getInquiryHistoryList($condition);
//        var_dump($info_list);die;
        $this->assign('info_list', $info_list);
        $this->assign('config_list', $config_list);
        $this->setAdminCurItem('inquiry_history');
        return $this->fetch('inquiry_history');
    }


    /**
     * @description: 动态获取工位  @MrLiu
     * @param InquiryStationModel $inquiryStationModel
     */
    public function get_station()
    {
        $inquiry_id = intval(input('param.inquiry_id'));
        $data = (new InquiryStationModel())->getStationByInquiryId($inquiry_id);
        if (empty($data)) {
            $this->error('请求数据为空！');
            throw new \Exception();
        }
        $this->success('请求成功', null, $data);
    }

    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'inquiry_info',
                'text' => lang('inquiry_list'),
                'url' => url('Goodinquiry/inquiry_info')
            ),
            array(
                'name' => 'inquiry_list',
                'text' => lang('inquiry_list_add'),
                'url' => url('Goodinquiry/inquiry_info_add')
            ),
            array(
                'name' => 'index',
                'text' => lang('inquiry_class'),
                'url' => url('Goodinquiry/index')
            ),
//            array(
//                'name' => 'inquiry_add',
//                'text' => lang('inquiry_class_add'),
//                'url' => "javascript:dsLayerOpen('".url('inquiry_add')."','".lang('inquiry_class_add')."')"
//            ),
            array(
                'name' => 'inquiry_station_list',
                'text' => lang('inquiry_station_list'),
                'url' => url('Goodinquiry/station_list')
            ),
            array(
                'name' => 'inquiry_history',
                'text' => lang('inquiry_history'),
                'url' =>  url('Goodinquiry/inquiry_history')
            ),

        );

        return $menu_array;
    }
}